package com.podmanager.data;

import com.podmanager.file.FileUtility;

import android.content.Context;
import android.os.AsyncTask;

public class DeleteSubscriptionAsyncTask extends
		AsyncTask<String, Void, String> {
	
	private Context context;
	public DeleteSubscriptionAsyncTask(Context c)
	{
		context = c;
	}
	@Override
	protected String doInBackground(String... params) {
		long sub_id = Long.parseLong(params[0]);
		try{//attempt deleting podcast directory
			FileUtility.deleteDirectory(FileUtility.getDirectoryForEpisodes(sub_id));
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		try{//attempt deleting episodes from episode table
			context.getContentResolver().delete(
					DBContentProvider.EPISODES_URI, 
					PMDatabase.EPISODES.SUB_ROW_ID +"="+sub_id, null);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		try{//attempt deleting subscription
			context.getContentResolver().delete(
					DBContentProvider.SUBSCRIPTION_URI, 
					PMDatabase.SUBSCRIPTIONS.KEY_ROW_ID +"="+sub_id, null);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

}
